package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.ElectronicExtendTwoInfoBo;
import com.hospital.workstation.domain.vo.ElectronicExtendTwoInfoVo;
import com.hospital.workstation.domain.ElectronicExtendTwoInfo;
import com.hospital.workstation.mapper.ElectronicExtendTwoInfoMapper;
import com.hospital.workstation.service.IElectronicExtendTwoInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 续二表Service业务层处理
 *
 * @author whb
 * @date 2024-05-07
 */
@RequiredArgsConstructor
@Service
public class ElectronicExtendTwoInfoServiceImpl implements IElectronicExtendTwoInfoService {

    private final ElectronicExtendTwoInfoMapper baseMapper;

    /**
     * 查询续二表
     */
    @Override
    public ElectronicExtendTwoInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询续二表列表
     */
    @Override
    public TableDataInfo<ElectronicExtendTwoInfoVo> queryPageList(ElectronicExtendTwoInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ElectronicExtendTwoInfo> lqw = buildQueryWrapper(bo);
        Page<ElectronicExtendTwoInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询续二表列表
     */
    @Override
    public List<ElectronicExtendTwoInfoVo> queryList(ElectronicExtendTwoInfoBo bo) {
        LambdaQueryWrapper<ElectronicExtendTwoInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ElectronicExtendTwoInfo> buildQueryWrapper(ElectronicExtendTwoInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ElectronicExtendTwoInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMonitorId() != null, ElectronicExtendTwoInfo::getMonitorId, bo.getMonitorId());
        lqw.eq(bo.getSerialNumber() != null, ElectronicExtendTwoInfo::getSerialNumber, bo.getSerialNumber());
        lqw.like(StringUtils.isNotBlank(bo.getPillsName()), ElectronicExtendTwoInfo::getPillsName, bo.getPillsName());
        lqw.eq(StringUtils.isNotBlank(bo.getGivePillsPathway()), ElectronicExtendTwoInfo::getGivePillsPathway, bo.getGivePillsPathway());
        lqw.eq(StringUtils.isNotBlank(bo.getPillsUsage()), ElectronicExtendTwoInfo::getPillsUsage, bo.getPillsUsage());
        lqw.eq(StringUtils.isNotBlank(bo.getPillsAmount()), ElectronicExtendTwoInfo::getPillsAmount, bo.getPillsAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getEndTime()), ElectronicExtendTwoInfo::getEndTime, bo.getEndTime());
        return lqw;
    }

    /**
     * 新增续二表
     */
    @Override
    public Boolean insertByBo(ElectronicExtendTwoInfoBo bo) {
        ElectronicExtendTwoInfo add = BeanUtil.toBean(bo, ElectronicExtendTwoInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改续二表
     */
    @Override
    public Boolean updateByBo(ElectronicExtendTwoInfoBo bo) {
        ElectronicExtendTwoInfo update = BeanUtil.toBean(bo, ElectronicExtendTwoInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ElectronicExtendTwoInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除续二表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
